﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SameGame
{
	/// <summary>
	/// Interaction logic for BallEllipseUserControl.xaml
	/// </summary>
	public partial class BallEllipseUserControl : UserControl
	{
		private int _colorType;

		private Color _color;
		private Color _highlightColor;

		private bool _isHighlighted;

		private static readonly Color _red;
		private static readonly Color _redLightGradient;
		private static readonly Color _green;
		private static readonly Color _greenLightGradient;
		private static readonly Color _blue;
		private static readonly Color _blueLightGradient;

		private static readonly float _highlightFactor;

		static BallEllipseUserControl()
		{
			_red = Color.FromArgb(0xFF, 0xC6, 0x19, 0x19);
			_redLightGradient = Color.FromArgb(0xFF, 0xF9, 0xB7, 0xB7);
			_green = Color.FromArgb(0xFF, 0x5B, 0xA8, 0x3A);
			_greenLightGradient = Color.FromArgb(0xFF, 0xD9, 0xF9, 0xBD);
			_blue = Color.FromArgb(0xFF, 0x5D, 0x55, 0xB0);
			_blueLightGradient = Color.FromArgb(0xFF, 0xC4, 0xC3, 0xFF);

			_highlightFactor = 1.8f;
		}

		public BallEllipseUserControl()
		{
			InitializeComponent();
		}

		public int ColorType
		{
			get
			{
				return _colorType;
			}
			set
			{
				_colorType = value;
				switch (_colorType)
				{
					case 0:
						ballColor.Color = _red;
						_color = _red;
						lightGradientStop.Color = _redLightGradient;
						break;
					case 1: //green
						ballColor.Color = _green;
						_color = _green;
						lightGradientStop.Color = _greenLightGradient;
						break;
					case 2: //blue
						ballColor.Color = _blue;
						_color = _blue;
						lightGradientStop.Color = _blueLightGradient;
						break;
					default:
						throw new Exception("This color is not supported: " + _colorType);
				}
				//_highlightColor = Color.Add(_color, Color.FromRgb(100, 100, 100));
				_highlightColor = Color.Multiply(_color, _highlightFactor);
			}
		}

		public bool IsHiLighted
		{
			get
			{
				return _isHighlighted;
			}
			set
			{
				_isHighlighted = value;
				OnHighlightedChanged();
			}
		}

		private void OnHighlightedChanged()
		{
			if (_isHighlighted)
			{
				if (ballColor.Color != _highlightColor)
				{
					ColorAnimation animation;

					animation = new ColorAnimation();
					animation.From = ballColor.Color;
					animation.To = _highlightColor;
					animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
					ballColor.BeginAnimation(GradientStop.ColorProperty, animation);
				}
			}
			else
			{
				if (ballColor.Color != _color)
				{
					ColorAnimation animation;

					animation = new ColorAnimation();
					animation.From = ballColor.Color;
					animation.To = _color;
					animation.Duration = new Duration(TimeSpan.FromSeconds(0.2));
					ballColor.BeginAnimation(GradientStop.ColorProperty, animation);
				}
			}
		}
	}
}
